<?php

/**
 * ECSHOP mobile前台公共函数
 * ============================================================================
 * * 版权所有 2005-2012 上海商派网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.ecshop.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: liuhui $
 * $Id: init.php 15013 2008-10-23 09:31:42Z liuhui $
*/


if (!defined('IN_ECS'))
{
    die('Hacking attempt');
}
define('ECS_WAP', true);


error_reporting(E_ALL);

if (__FILE__ == '')
{
    die('Fatal error code: 0');
}
// if ($_SERVER['HTTP_X_REAL_IP'] != '183.128.227.71') {
//     die('非法IP入侵');
// }

//第一个阿超；第二个邹Sir;第三个欧阳
//if ($_SERVER['REMOTE_ADDR'] != '60.186.81.50' && $_SERVER['HTTP_X_REAL_IP'] != '125.118.118.161') {
//    echo $_SERVER['HTTP_X_REAL_IP'];
//    die('非法IP入侵');
//}



// if($_SERVER['REMOTE_ADDR'] != '61.140.48.481'){
// header("Content-type: text/html; charset=utf-8");     
// die('正在排队中，请稍候！');
// }

/* 取得当前所在的根目录 */
define('ROOT_PATH', str_replace('mobile/includes/init.php', '', str_replace('\\', '/', __FILE__)));
define('IS_MOBILE', true);

// 加载列队
// require(ROOT_PATH . 'includes/lib_wait.php');
// wait('mobile');// 启动列队



/* 初始化设置 */
@ini_set('memory_limit',          '64M');
@ini_set('session.cache_expire',  180);
@ini_set('session.use_cookies',   1);
@ini_set('session.auto_start',    0);
@ini_set('display_errors',        0);
@ini_set("arg_separator.output","&amp;");

if (DIRECTORY_SEPARATOR == '\\')
{
    @ini_set('include_path',      '.;' . ROOT_PATH);
}
else
{
    @ini_set('include_path',      '.:' . ROOT_PATH);
}

if (file_exists(ROOT_PATH . 'data/config.php'))
{
    include(ROOT_PATH . 'data/config.php');
}
else
{
    include(ROOT_PATH . 'includes/config.php');
}

if (defined('DEBUG_MODE') == false)
{
    define('DEBUG_MODE', 0);
}

if (PHP_VERSION >= '5.1' && !empty($timezone))
{
    date_default_timezone_set($timezone);
}

$php_self = isset($_SERVER['PHP_SELF']) ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME'];
if ('/' == substr($php_self, -1))
{
    $php_self .= 'index.php';
}
define('PHP_SELF', $php_self);


require(ROOT_PATH . 'includes/funBase.php');
require(ROOT_PATH . 'includes/funCommon.php');
require(ROOT_PATH . 'mobile/includes/inc_constant.php');
require(ROOT_PATH . 'mobile/includes/cls_ecshop.php');
require(ROOT_PATH . 'mobile/includes/cls_error.php');
require(ROOT_PATH . 'mobile/includes/lib_time.php');
require(ROOT_PATH . 'mobile/includes/lib_base.php');
require(ROOT_PATH . 'mobile/includes/lib_common.php');
require(ROOT_PATH . 'mobile/includes/lib_insert.php');
require(ROOT_PATH . 'mobile/includes/lib_goods.php');
require(ROOT_PATH . 'mobile/includes/lib_article.php');
require(ROOT_PATH . 'mobile/includes/lib_main.php');




/* 对用户传入的变量进行转义操作。*/
if (!get_magic_quotes_gpc())
{
    if (!empty($_GET))
    {
        $_GET  = addslashes_deep($_GET);
    }
    if (!empty($_POST))
    {
        $_POST = addslashes_deep($_POST);
    }

    $_COOKIE   = addslashes_deep($_COOKIE);
    $_REQUEST  = addslashes_deep($_REQUEST);
}


/* 初始化FTP类库 */
// require(ROOT_PATH . 'includes/classFtp.php');
// $ftp = new classFtp($ftpConfig);

// 实例化mem类
$mem = new memcached();
// 以二进制进行传输，当要进行密码验证时，这个一定要为true
$mem->setOption(Memcached::OPT_BINARY_PROTOCOL, true);
// 添加服务池
$mem->addServer('512145b6f8ea49a5.m.cnqdalicm9pub001.ocs.aliyuncs.com', '11211');
// 进行密码验证
$mem->setSaslAuthData('512145b6f8ea49a5','Eggms520');
// 取消自动压缩，如果要后面追加数据，不可压缩数据
$mem->setOption(Memcached::OPT_COMPRESSION, false);
$mem->increment('concurrency');

    /* 创建新对象 */
    $ecs = new ECS($_CFG['dbName'], $_CFG['prefix']);
    define('DATA_DIR', $ecs->data_dir());
    define('IMAGE_DIR', $ecs->image_dir());

    /* 初始化新版数据库类 */
    require(ROOT_PATH . 'includes/classMysqli.php');
    $db = new classMysqli($_CFG['dbHost'], $_CFG['dbUser'], $_CFG['dbPass'], $_CFG['dbName'],$_CFG['charset'],$_CFG['prefix'], 0, $_CFG['dbRead']);

    $db->setMaxCacheTime($_CFG['dbCacheTime']);

/* 载入系统参数 */
$_CFG = array_merge($_CFG,load_config());

/* 载入语言文件 */
require(ROOT_PATH . 'languages/' . $_CFG['lang'] . '/common.php');

if ($_CFG['shop_closed'] == 1)
{
    /* 商店关闭了，输出关闭的消息 */
    header('Content-type: text/html; charset='.EC_CHARSET);

    die('<div style="margin: 150px; text-align: center; font-size: 14px"><p>' . $_LANG['shop_closed'] . '</p><p>' . $_CFG['close_comment'] . '</p></div>');
}


/* 初始化session */
    ini_set("session.cookie_domain", ".eggms.com");
    ini_set("session.gc_maxlifetime", "1440");
    session_start();
    define('SESS_ID', session_id());


/* 当程序执行完成后执行concurrency函数 */
register_shutdown_function('concurrency');
function concurrency(){
    $GLOBALS['mem']->decrement('concurrency');
    $GLOBALS['mem']->decrement(md5(SESS_ID));
}

$mem->increment('concurrency');
$mem->add(md5(SESS_ID),0,3600);
if ($mem->increment(md5(SESS_ID)) > 3) {
    header('location: wait.php');
    exit;
}


if (!defined('INIT_NO_SMARTY'))
{
    header('Cache-control: private');
    header('Content-type: text/html; charset=utf-8');

    if ($php_self != '/mobile/buyone.php') {

        /* 创建 Smarty 对象。*/
        require(ROOT_PATH . 'includes/cls_template.php');
        $smarty = new cls_template;

        $smarty->cache_lifetime = $_CFG['cache_time'];
        $smarty->template_dir   = ROOT_PATH . 'mobile/templates';
        $smarty->cache_dir      = ROOT_PATH . 'temp/caches/mobile';
        $smarty->compile_dir    = ROOT_PATH . 'temp/compiled/mobile';

    } else {
        /* 加载最新模板引擎类 */
        require(ROOT_PATH . 'includes/smarty-3.1.24/Smarty.class.php');
        $smarty = new smarty();
        // 用cacheLifeTime来判断缓存文件是否过期。
        // $smarty->setCaching(Smarty::CACHING_LIFETIME_CURRENT);
        // 禁用模板缓存
        $smarty->setCaching(false);
        // 设置模板目录
        $smarty->setTemplateDir(ROOT_PATH . 'mobile/templates');
        // 设置模板缓存编译目录
        $smarty->setCompileDir(ROOT_PATH . 'temp/compiled/mobile');
        // 设置模板缓存目录
        $smarty->setCacheDir(ROOT_PATH . 'temp/caches/mobile');
        // 设置缓存过期时间
        $smarty->setCacheLifetime($_CFG['cache_time']);
        // 设置调试模式
        $smarty->debugging = false;

        $smarty->assign('__ISNEW__', true);
    }

    $smarty->assign('__PUC__', __PUC__);
    $smarty->assign('__PUCM__', __PUCM__);
    $smarty->assign('__IMG__', __IMG__);
    $smarty->assign('url', 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
    $egg_img = 'http://img01.eggms.com/';
    $smarty->assign('egg_img', $egg_img);
}





if (!defined('INIT_NO_USERS'))
{
    /* 会员信息 */
    $user =& init_users();

    if (empty($_SESSION['user_id']))
    {
        if ($user->get_cookie())
        {
            /* 如果会员已经登录并且还没有获得会员的帐户余额、积分以及优惠券 */
            if ($_SESSION['user_id'] > 0)
            {
                update_user_info();
            }
        }
        else
        {
            $_SESSION['user_id']     = 0;
            $_SESSION['user_name']   = '';
            $_SESSION['email']       = '';
            $_SESSION['user_rank']   = 0;
            $_SESSION['discount']    = 1.00;
            if (!isset($_SESSION['login_fail']))
            {
                $_SESSION['login_fail'] = 0;
            }
        }
    }

    if (isset($smarty))
    {
        $smarty->assign('ecs_session', $_SESSION);
    }
}
if ((DEBUG_MODE & 1) == 1)
{
    error_reporting(E_ALL);
}
else
{
    error_reporting(E_ALL ^ E_NOTICE);
}
if ((DEBUG_MODE & 4) == 4)
{
    include(ROOT_PATH . 'includes/lib.debug.php');
}

//百度统计
if (!defined('INIT_NO_SMARTY'))
{
    require(ROOT_PATH . "mobile/hm.php");
    $_hmt = new _HMT("b80243b525d543c48564b56c7313c9be");
    $_hmtPixel = $_hmt->trackPageView();
    $smarty->assign('_hmtPixel' , $_hmtPixel);
}

/* 判断是否支持gzip模式 */
if (gzip_enabled())
{
    ob_start('ob_gzhandler');
}
require(ROOT_PATH . 'includes/classStock.php');
$virtualStock = new classStock();
/* wap头文件 */
header("Content-Type:text/html; charset=utf-8");


/*宝藏跳转*/
$nest_url = $_SERVER['HTTP_REFERER'];//上页
$now_url = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
$header_url = 'http://'.$_SERVER['SERVER_NAME'].'/mobile/';
if ($nest_url == $header_url.'index.php'&&$now_url ==$header_url.'search.php?keywords=大千' ) {
    $parent_id = 54;
    $key = '1223123';
    gift_stock($parent_id,$key);
}
if ($nest_url == $header_url.'freeplan.php'&&$now_url ==$header_url.'freeplan.php?act=talent_plan' ) {
    $parent_id = 55;
    $key = 'faefq231';
    gift_stock($parent_id,$key);
}
if ($nest_url == $header_url.'found.php'&&$now_url ==$header_url.'tvd.php?id=99' ) {
    $parent_id = 56;
    $key = 'fbdry542';
    gift_stock($parent_id,$key);
}
if ($nest_url == $header_url.'topic.php'&&$now_url ==$header_url.'tvdList.php?cid=8' ) {
    $parent_id = 57;
    $key = 'bsdvgfhj211';
    gift_stock($parent_id,$key);
}
if ($nest_url == $header_url.'found.php'&&$now_url ==$header_url.'found.php?act=nav&num=4' ) {
    $parent_id = 58;
    $key = 'bsdgdrfh143';
    gift_stock($parent_id,$key);
}

function gift_stock($parent_id,$key)
{
    $sql = "SELECT valid_number from ".$GLOBALS['ecs']->table('activity_stock') . " WHERE parent_id = '$parent_id'";
    $stock = $GLOBALS['db']->getOne($sql);
    if ($stock>0) {
        $back_url = 'http://'.$_SERVER['SERVER_NAME'].'/mobile/free_goods.php?act=info&key='.$key;
        header("Location: ".$back_url);
        return $back_url;
    }
    return false;
}
?>